/**
 * Created by user on 2018/12/5.
 */
const utils = require('./src/utils')
const { DEFAULT_PAGE_COUNT, DEFAULT_ROOM_ID } = require('./src/config')
const puppeteer = require('puppeteer')
const program = require('commander')
const moment = require('moment')
program.version('0.0.1')
    .option('-c, --count [value]', '开启页面数', parseInt)
    .option('-r, --room [value]', '房间id', parseInt)
    .option('-t, --test [value]', '是否测试房间', parseInt)
    .option('-l, --headless [value]', '是否headless', v => !!parseInt(v))
    .parse(process.argv)
const { count: pageCount = DEFAULT_PAGE_COUNT, test: isTest = 1,
    room: roomId = DEFAULT_ROOM_ID, headless: inputHeadless = true } = program

console.log(pageCount, roomId, inputHeadless, isTest)
// return

const pageUrl = utils.getPageUrl(roomId, isTest)

let puppeteerConfig = utils.getPuppeteerConfig({headless: inputHeadless})
function sleep(time = 1) {
    return new Promise(function(resolve) {
        resolve()
    }, time * 1000)
}
async function start(count = 10) {
    const browser = await (puppeteer.launch(puppeteerConfig))
    let p = 1
    while (p <= count) {
        let page = await browser.newPage()
        await page.goto(pageUrl)
        await sleep(1)
        console.log('start page: %s, %s' , p, moment().format('HH:mm:ss'))
        p += 1
    }
    // await Promise.all(pages)
    console.log('all start')
}

start(pageCount)
